package com.auerdi.ui.fragment.e_my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auerdi.R;
import com.auerdi.base.BaseMainFragment;
import com.auerdi.ui.fragment.e_my.orders.ADOrdersTabFragment;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.AccountInfo;
import com.example.egghttp.retrofit.beans.banner.AccountInfoList;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by YoKeyword on 16/6/30.
 */
public class AdMeTabFragment extends BaseMainFragment {

    Unbinder unbinder;
    @BindView(R.id.header)
    LinearLayout header;
    @BindView(R.id.fl_container)
    RelativeLayout flContainer;
    @BindView(R.id.goodName)
    TextView goodName;
    @BindView(R.id.order_a_pay)
    RelativeLayout orderAPay;
    @BindView(R.id.order_b_send)
    RelativeLayout orderBSend;
    @BindView(R.id.order_c_receive)
    RelativeLayout orderCReceive;
    @BindView(R.id.order_d_group)
    RelativeLayout orderDGroup;
    @BindView(R.id.order_e_return)
    RelativeLayout orderEReturn;

    public static AdMeTabFragment newInstance() {

        Bundle args = new Bundle();
        AdMeTabFragment fragment = new AdMeTabFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab_me, container, false);
        unbinder = ButterKnife.bind(this, view);

        orderAPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(ADOrdersTabFragment.newInstance(0));
            }
        });


        orderBSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(ADOrdersTabFragment.newInstance(1));
            }
        });

        orderCReceive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(ADOrdersTabFragment.newInstance(2));
            }
        });

        orderDGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(ADOrdersTabFragment.newInstance(3));
            }
        });

        orderEReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(ADOrdersTabFragment.newInstance(4));
            }
        });

        header.setOnClickListener(v -> RetrofitClient.getClient().create(AdService.class).userAccount()
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BOb<AccountInfoList>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        L.d("hcia", "e:" + e);


                    }

                    @Override
                    public void onNext(BOb<AccountInfoList> bannerListBOb) {

                        AccountInfo tm = bannerListBOb.getBody().getAccount_info();

                        L.d("hcia", " tm.getUid():" + tm.getUid());
                        L.d("hcia", " tm.getNickname():" + tm.getNickname());

//                                EventBus.getDefault().post(new LoginEvent(401, "需登入"));


                    }
                }));


//        L.d("hcia", "view:" + view);


        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
//        L.d("hcia", "onResume:");

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
//        L.d("hcia", "savedInstanceState:" + savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
